package com.travel.server.manager.impl.command.customer;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.customer.QueryMemberGradeListResponse;
import com.travel.server.manager.dto.response.customer.QueryMemberGradeListResponse.MemberGradeItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.MemberGradeDal;
import com.travel.server.manager.impl.dal.MemberGradeDal.MemberGradeParam;
import com.travel.server.manager.model.MemberGradeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:07
 * @Desc:
 */
@Component
@Slf4j
public class QueryMemberGradeListCommand extends AbstractCommand<EmptyRequest, QueryMemberGradeListResponse> {

    @Autowired
    private MemberGradeDal memberGradeDal;

    @Override
    protected void checkInput(EmptyRequest request) {
    }

    @Override
    public QueryMemberGradeListResponse handle(EmptyRequest request) {
        QueryMemberGradeListResponse response = new QueryMemberGradeListResponse();

        MemberGradeParam param = new MemberGradeParam();
        List<MemberGradeInfo> list = memberGradeDal.selectByExample(param);
        if(CollectionUtil.isEmpty(list)){
            return response;
        }
        List<MemberGradeItem> memberGradeList = new ArrayList<>();
        for(MemberGradeInfo memberGradeInfo : list){
            MemberGradeItem item = new MemberGradeItem();
            item.setGradeName(memberGradeInfo.getName());
            item.setId(memberGradeInfo.getId());
            item.setIsDefault(memberGradeInfo.getIsDefault());
            item.setScoreLimit(memberGradeInfo.getScoreLimit());
            item.setMemberRight(memberGradeInfo.getMemberRight());
            memberGradeList.add(item);
        }
        response.setTotalNum(list.size());
        response.setGradeList(memberGradeList);

        return response;
    }
}
